import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:whale/helper/utils/util.dart';

import '../index.dart';
import '../model/model.dart';

/// hello
class ReportHeaderView extends GetView<MeansDetailController> {
  const ReportHeaderView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var state = controller.state;
    return GetBuilder<MeansDetailController>(
      builder: (_) => Container(
        padding: EdgeInsets.symmetric(horizontal: 12.r),
        decoration: const BoxDecoration(
            color: Colors.white,
            border:
                Border(bottom: BorderSide(color: ColorsUtil.bg, width: 0.5))),
        child: Stack(
          children: [
            Container(
              decoration: const BoxDecoration(
                border: Border(
                  bottom: BorderSide(color: ColorsUtil.bg, width: 0.5),
                ),
              ),
              padding: EdgeInsets.symmetric(vertical: 20.r),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    state.mod?.documentTitle ?? '',
                    style: TextStyle(
                        color: ColorsUtil.title,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.bold),
                    // textAlign: TextAlign.center,
                  ),
                  Visibility(
                    visible: state.mod?.reportType == '政策措施' ||
                        state.mod?.reportType == '招标采购',
                    child: Column(
                      children: [
                        SizedBox(
                          height: 10.r,
                        ),
                        Row(
                          // mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 6, vertical: 3),
                              color: state.mod?.riskColors.last,
                              child: Text(
                                state.mod?.riskLevel ?? '',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 10.sp,
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 10.r,
                            ),
                            Container(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 8, vertical: 3),
                              color: ColorsUtil.bg,
                              child: Text(
                                state.mod?.publishTime ?? '-',
                                style: TextStyle(
                                    color: ColorsUtil.content, fontSize: 10.sp),
                              ),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                  Visibility(
                    visible: !isZeroList(state.titles),
                    child: Container(
                      color: ColorsUtil.bg,
                      margin: EdgeInsets.only(top: 15.r),
                      child: Column(
                        children: [
                          Wrap(
                            // spacing: 10.r,
                            // runSpacing: 10.r,
                            children: state.titles
                                    ?.asMap()
                                    .map((idx, e) => MapEntry(
                                          idx,
                                          TextTopDownView(
                                            top: e.keys.first,
                                            bottom: e.values.first,
                                            width: (Get.width - 24.r) / 2,
                                            hasRightLine: idx % 2 == 0,
                                          ),
                                        ))
                                    .values
                                    .toList() ??
                                [],
                          ),
                          Column(
                            children: state.titles1
                                    ?.map((e) => _getItems3(e))
                                    .toList() ??
                                [],
                          ),
                          SizedBox(
                            height: 10.r,
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            Visibility(
              visible: state.mod?.hasProblem == '1',
              child: Positioned(
                right: 0,
                top: 80.r,
                child: loadImage(
                    url: 'deregulation.png'.imgPathMeans, width: 100.r),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _getItems3(List<Map<String, String>> list) {
    if (list.length < 3) {
      return const SizedBox();
    }
    return Row(
      children: [
        Flexible(
          flex: 2,
          child: TextTopDownView(
            top: list[0].keys.first,
            bottom: list[0].values.first,
            hasRightLine: true,
          ),
        ),
        Flexible(
          flex: 1,
          child: TextTopDownView(
            top: list[1].keys.first,
            bottom: list[1].values.first,
            hasRightLine: true,
          ),
        ),
        Flexible(
          flex: 1,
          child: TextTopDownView(
              top: list[2].keys.first, bottom: list[2].values.first),
        ),
      ],
    );
  }
}

class TextTopDownView extends StatelessWidget {
  const TextTopDownView({
    Key? key,
    required this.top,
    required this.bottom,
    this.hasRightLine = false,
    this.width,
  }) : super(key: key);
  final String top;
  final String bottom;
  final double? width;
  final bool hasRightLine;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 15.r),
      margin: EdgeInsets.only(top: 10.r),
      width: width,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 18.r,
                  child: Text(
                    top,
                    style: TextStyle(
                      color: ColorsUtil.content,
                      fontSize: 11.sp,
                    ),
                  ),
                ),
                Container(
                  height: 18.r,
                  alignment: Alignment.bottomLeft,
                  child: AutoSizeText(
                    bottom,
                    textAlign: TextAlign.center,
                    maxLines: 1,
                    minFontSize: 10,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(
                      color: ColorsUtil.title,
                    ),
                  ),
                ),
              ],
            ),
          ),
          hasRightLine
              ? Container(
                  color: Colors.grey,
                  width: 0.5,
                  height: 25.r,
                )
              : const SizedBox(),
        ],
      ),
    );
  }
}
